package com.llkj.qingnianguoxue.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.PreviousActionAdapter;
import com.llkj.qingnianguoxue.base.BaseFragment;
import com.llkj.qingnianguoxue.bean.PreviousActionBean;
import com.llkj.qingnianguoxue.bean.RxCoursePayBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.AgreementActivity;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

/**
 * 范儿活动
 */
public class ActionFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    private static final String CATEGORYTYPE = "category_type";
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvActions;
    private PreviousActionAdapter actionAdapter;
    private List<PreviousActionBean.ActionBean> actionData;
    private int totalSize, mPage;
    private int mCategoryType;

    public static ActionFragment newInstance(int param1) {
        ActionFragment fragment = new ActionFragment();
        Bundle args = new Bundle();
        args.putInt(CATEGORYTYPE, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_action, container, false);

        Bundle bundle = getArguments();
        mCategoryType = bundle.getInt(CATEGORYTYPE);

        initView();
        initListener();

//        loadWaitDialog();
        onRefresh();
        return rootView;
    }

    private void initView() {
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvActions = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        lvActions.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        actionData = new ArrayList<>();
        actionAdapter = new PreviousActionAdapter(R.layout.item_previous_action, actionData);
        if(mCategoryType==2){
            actionAdapter.setIsFee(true);
        }
        lvActions.setAdapter(actionAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        actionAdapter.setOnLoadMoreListener(this);
        lvActions.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                PreviousActionBean.ActionBean item = actionData.get(i);
                if ("1".equals(item.getType())) {// 类型 1 内部 2 外部
                    Intent intent = new Intent(getActivity(), ActionDetailActivity.class);
                    intent.putExtra("from_class", 1);
                    intent.putExtra("id", item.getId());
                    startActivity(intent);
                } else {//外部详情要加载网页
                    Bundle bundle = new Bundle();
                    bundle.putInt("from_class", 4);
                    bundle.putString(AgreementActivity.WEB_URL,item.getLink());
                    startActivity(AgreementActivity.class, bundle);
                }
            }
        });
        RxBus.getInstance().toObservable(RxCoursePayBean.class).subscribe(new Action1<RxCoursePayBean>() {
            @Override
            public void call(RxCoursePayBean payBean) {
                if (payBean.isPaySuccess){
                   onRefresh();
                }
            }
        });
    }
    @Override
    public void onLoadMore() {

        mPage++;
        getActions();
    }

    @Override
    public void onRefresh() {
        mPage=1;
        getActions();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, actionAdapter, null, lvActions, totalSize);
    }

    private void getActions() {
        HttpMethod.hotAction(getActivity(), this, String.valueOf(mPage), String.valueOf(mCategoryType));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_INDEX_HOT_ACTIVITY) {
            PreviousActionBean hotRankingBean = GsonUtil.GsonToBean(data, PreviousActionBean.class);
            if (hotRankingBean != null) {
                totalSize = Integer.parseInt(hotRankingBean.getNum());

                if (mPage == 1) {
                    actionData.clear();
                    LoadDataHelper.refreshComplete(actionAdapter, actionData, refreshLayout, lvActions);
                } else {
                    actionAdapter.setEnableLoadMore(true);
                    actionAdapter.loadMoreComplete();
                }
                actionData.addAll(hotRankingBean.getActivity());
                actionAdapter.notifyDataSetChanged();
            }
        }
    }
}
